// pages/index/customOptions/customOptions.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customOptionList: [],
    // fabricColor: [],
    customList: [],
    wordInput: [],
    productId: ""
  },

  // 选择对应体型
  selectPart: function(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    this.setData({
      partIndex: index
    })
  },

  // 选择对应目标
  selectPartStyle: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    that.setData({
      targetIndex: index
    })
    for (let i = 0; i < that.data.customOptionList[that.data.partIndex].paramOptions.length; i++) {
      if (that.data.targetIndex == i) {
        that.data.customOptionList[that.data.partIndex].paramOptions[i].select = false
      } else {
        that.data.customOptionList[that.data.partIndex].paramOptions[i].select = true
      }
      that.setData(that.data)
    }
  },

  // // 选择身体特征
  // selectFeature: function(e) {
  //   let that = this
  //   let index = e.currentTarget.dataset.index
  //   let item = that.data.bodyFeature[index]
  //   item.isChecked = !item.isChecked
  //   that.setData({
  //     bodyFeature: that.data.bodyFeature
  //   });
  //   let arr = []
  //   for (let i = 0; i < that.data.bodyFeature.length; i++) {
  //     if (that.data.bodyFeature[i].isChecked == true) {
  //       // console.log(that.data.bodyFeature[i].id)
  //       let text = that.data.bodyFeature[i].text
  //       arr.push(text)
  //       that.setData({
  //         arr: arr
  //       })
  //     }
  //     // console.log(arr)
  //   }
  // },

  // 获取刺绣内容
  wordInput: function(e) {
    let that = this
    // console.log(e.detail.value)
    let value = {
      paramName: "刺绣内容",
      option: e.detail.value
    }
    that.data.wordInput.push(value)
    that.setData(that.data)
    // console.log(that.data.wordInput)
  },

  // 下一步
  next: function(e) {
    let that = this
    // 获取用户选中的元素
    let list = []
    list = that.data.customOptionList
    
    for (let i = 0; i < list.length; i++) {
      console.log(list[i].paramOptions)
      for (let j = 0; j < list[i].paramOptions.length; j++) {
        if (list[i].paramOptions[j].select == false) {
          let custom = {
            paramName: list[i].paramName,
            option: list[i].paramOptions[j].option
          }
          that.data.customList.push(custom)
        }
      }
    }
    // that.data.customList.push(that.data.fabricColor)
    if(that.data.wordInput!=""){
      that.data.wordInput = that.data.wordInput[that.data.wordInput.length - 1]
    }
    
    that.data.customList = that.data.customList.concat(that.data.wordInput)
    that.setData(that.data)
    // console.log(that.data.customList)
    // 将定制信息传回后台
    let msgUrl = app.globalData.baseUrl + "weixin/saveMsgTemp"
    let data = {
      cid: wx.getStorageSync('cid'),
      msg: that.data.customList,
      type: 2
    }
    util.requestLoading(msgUrl, data, "GET").then(res => {
      // console.log(res)
      wx.navigateTo({
        url: '../../mine/mySize/mySize?productId=' + that.data.productId
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    console.log(options.productId)
    that.setData({
      productId: options.productId
    })
    // console.log(that.data.productId)
    // // 请求特殊定制列表
    // let customUrl = app.globalData.baseUrl + "weixin/customOptionList"
    // util.requestLoading(customUrl, null, "GET").then(res => {
    //   // console.log(res.data.customOptionList)
    //   this.setData({
    //     customOptionList: res.data.customOptionList
    //   })
    // })
  },

  onShow:function(){
    let that = this
    that.setData({
      customList: [],
      // productId: options.productId
    })
    // console.log(that.data.productId)
    // 请求特殊定制列表
    let customUrl = app.globalData.baseUrl + "weixin/customOptionList"
    util.requestLoading(customUrl, null, "GET").then(res => {
      console.log(res.data.customOptionList)
      this.setData({
        customOptionList: res.data.customOptionList
      })
    })
  }
})